/**************************************************************************
 *                                                                        *
 *         Copyright (c) 2014 by Generalplus Inc.                         *
 *                                                                        *
 *  This software is copyrighted by and is the property of Generalplus    *
 *  Inc. All rights are reserved by Generalplus Inc.                      *
 *  This software may only be used in accordance with the                 *
 *  corresponding license agreement. Any unauthorized use, duplication,   *
 *  distribution, or disclosure of this software is expressly forbidden.  *
 *                                                                        *
 *  This Copyright notice MUST not be removed or modified without prior   *
 *  written consent of Generalplus Technology Co., Ltd.                   *
 *                                                                        *
 *  Generalplus Inc. reserves the right to modify this software           *
 *  without notice.                                                       *
 *                                                                        *
 *  Generalplus Inc.                                                      *
 *  No.19, Industry E. Rd. IV, Hsinchu Science Park                       *
 *  Hsinchu City 30078, Taiwan, R.O.C.                                    *
 *                                                                        *
 **************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "drv_l1_sfr.h"
#include "drv_l1_mipi.h"
#include "drv_l1_i2c.h"
#include "drv_l2_sensor.h"
#include "drv_l2_sccb.h"
#include "drv_l2_cdsp.h"
#include "drv_l2_cdsp_config.h"
#include "gplib_print_string.h"

#if (defined _SENSOR_OV9750_CDSP_MIPI) && (_SENSOR_OV9750_CDSP_MIPI == 1)

#include "drv_l2_user_calibration.h"

/**************************************************************************
 *                           C O N S T A N T S                            *
 **************************************************************************/
#ifndef DISABLE
#define DISABLE 0
#endif
#ifndef ENABLE
#define ENABLE	1
#endif

#define MIPI_ISR_TEST				0
#define MIPI_LANE_NO				MIPI_1_LANE // 1 or 2 lane
#if (MIPI_LANE_NO == MIPI_2_LANE)
#define MIPI_DEV_NO 				0				//2Lane only selcet MIPI_0
#else
#define MIPI_DEV_NO					0				//1Lane can 0:MIPI_0 or 1:MIPI_1
#endif

#define	OV9750_ID					0x42

/*MIPI Rx Input & ISP Input*/
#define OV9750_WIDTH				1280
#define OV9750_HEIGHT				720

/*ISP Output*/
#define OV9750_OUT_WIDTH			1280
#define OV9750_OUT_HEIGHT			720


#define OV9750_25FPS_50HZ_DAY_EV_IDX			140
#define OV9750_25FPS_50HZ_NIGHT_EV_IDX		    200
#define OV9750_25FPS_50HZ_EXP_TIME_TOTAL		(262-19)
#define OV9750_25FPS_50HZ_INIT_EV_IDX			(OV9750_25FPS_50HZ_EXP_TIME_TOTAL >> 1)
#define OV9750_25FPS_50HZ_MAX_EV_IDX			(OV9750_25FPS_50HZ_EXP_TIME_TOTAL)


#define OV9750_25FPS_60HZ_DAY_EV_IDX			140
#define OV9750_25FPS_60HZ_NIGHT_EV_IDX		    200
#define OV9750_25FPS_60HZ_EXP_TIME_TOTAL		(257-19)
#define OV9750_25FPS_60HZ_INIT_EV_IDX			(OV9750_25FPS_60HZ_EXP_TIME_TOTAL >> 1)
#define OV9750_25FPS_60HZ_MAX_EV_IDX			(OV9750_25FPS_60HZ_EXP_TIME_TOTAL)

/**************************************************************************
 *                         G L O B A L    D A T A                         *
 **************************************************************************/

static int					*p_expTime_table;
static sensor_exposure_t	seInfo;
static int					pre_sensor_a_gain, pre_sensor_time;

#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW

sccb_config_t	ov9750_handle =
{
	SENSOR_I2C_SCL,		//scl_port
	SENSOR_I2C_DRIVING,	//scl_drv
	SENSOR_I2C_SDA,		//sda_port
	SENSOR_I2C_DRIVING,	//sda_drv
	0,					//pwdn_port
	0,					//pwdn_drv
	0,					//have_pwdn
	16,					//RegBits
	8,					//DataBits
	OV9750_ID,			//slaveAddr
	0x20000,			//timeout
	100					//clock_rate
};

#elif SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW

static drv_l1_i2c_bus_handle_t	ov9750_handle =
{
	SENSOR_I2C_DEV,
	OV9750_ID,
	100
};
#endif


static mipi_config_t ov9750_mipi_cfg =
{
	DISABLE,			/* low power, 0:disable, 1:enable */

#if MIPI_LANE_NO == MIPI_1_LANE
	D_PHY_SAMPLE_POS,	/* byte clock edge, 0:posedge, 1:negedge */
	MIPI_1_LANE,		/* lane */
	D_PHY_BYTE_CLK,		/* byte clock source */
#elif MIPI_LANE_NO == MIPI_2_LANE
	D_PHY_SAMPLE_POS,
	MIPI_2_LANE,		/* lane */
	GENERATE_PIXEL_CLK, /* byte clock source */
#endif
	MIPI_AUTO_DETECT,	/* data mode, 0:auto detect, 1:user define */
	MIPI_RAW10,			/* data type, valid when data mode is 1*/
	MIPI_DATA_TO_CDSP,	/* data type, 1:data[7:0]+2':00 to cdsp, 0: 2'00+data[7:0] to csi */
	0,					//NULL,			/* RSD 2 */
	OV9750_WIDTH,		/* width, 0~0xFFFF */
	OV9750_HEIGHT,		/* height, 0~0xFFFF */
#if SENSOR_FLIP
	0,
	4,
#else
	0,					/* back porch, 0~0xF *///If Set 4 have show paper color at left side.u2.20160119
	4,
#endif
	ENABLE,				/* blanking_line_en, 0:disable, 1:enable */
	0,					//NULL,			/* RSD 3 */
	ENABLE,				/* ecc, 0:disable, 1:enable */
	MIPI_ECC_ORDER3,	/* ecc order */

	100,/* data mask, unit is ns */ /*250 for 48Mhz, 1400 for 40Mhz*/

	MIPI_CHECK_HS_SEQ	//MIPI_CHECK_LP_00	/* check hs sequence or LP00 for clock lane */
};


static const regval16_t	ov9750_init_table[] =
{
	{0x0103, 0x01},
	
	{0xFFFF, 0xF0},//I2C_DELAY
		
	{0x0103, 0x00},
		
	{0x0100, 0x00},
	{0x0300, 0x04},
	{0x0302, 0x64},
	{0x0303, 0x00},
	{0x0304, 0x03},
	{0x0305, 0x01},
	{0x0306, 0x01},
	{0x030a, 0x00},
	{0x030b, 0x00},
	{0x030d, 0x1e},
	{0x030e, 0x01},
	{0x030f, 0x04},
	{0x0312, 0x01},
	{0x031e, 0x04},
	{0x3000, 0x00},
	{0x3001, 0x00},
	{0x3002, 0x21},
	{0x3005, 0xf0},
	{0x3011, 0x00},
	{0x3016, 0x53},
	{0x3018, 0x12},
	{0x301a, 0xf0},
	{0x301b, 0xf0},
	{0x301c, 0xf0},
	{0x301d, 0xf0},
	{0x301e, 0xf0},
	{0x3022, 0x01},
	{0x3031, 0x0a},
	{0x3032, 0x80},
	{0x303c, 0xff},
	{0x303e, 0xff},
	{0x3040, 0xf0},
	{0x3041, 0x00},
	{0x3042, 0xf0},
	{0x3104, 0x01},
	{0x3106, 0x15},
	{0x3107, 0x01},
	{0x3500, 0x00},
	{0x3501, 0x3d},
	{0x3502, 0x00},
	{0x3503, 0x08},
	{0x3504, 0x03},
	{0x3505, 0x83},
	{0x3508, 0x02},
	{0x3509, 0x80},
	{0x3600, 0x65},
	{0x3601, 0x60},
	{0x3602, 0x22},
	{0x3610, 0xe8},
	{0x3611, 0x56},
	{0x3612, 0x48},
	{0x3613, 0x5a},
	{0x3614, 0x91},
	{0x3615, 0x79},
	{0x3617, 0x57},
	{0x3621, 0x90},
	{0x3622, 0x00},
	{0x3623, 0x00},
	{0x3625, 0x07},
	{0x3633, 0x10},
	{0x3634, 0x10},
	{0x3635, 0x14},
	{0x3636, 0x13},
	{0x3650, 0x00},
	{0x3652, 0xff},
	{0x3654, 0x00},
	{0x3653, 0x34},
	{0x3655, 0x20},
	{0x3656, 0xff},
	{0x3657, 0xc4},
	{0x365a, 0xff},
	{0x365b, 0xff},
	{0x365e, 0xff},
	{0x365f, 0x00},
	{0x3668, 0x00},
	{0x366a, 0x07},
	{0x366d, 0x00},
	{0x366e, 0x10},
	{0x3702, 0x1d},
	{0x3703, 0x10},
	{0x3704, 0x14},
	{0x3705, 0x00},
	{0x3706, 0x27},
	{0x3709, 0x24},
	{0x370a, 0x00},
	{0x370b, 0x7d},
	{0x3714, 0x24},
	{0x371a, 0x5e},
	{0x3730, 0x82},
	{0x3733, 0x10},
	{0x373e, 0x18},
	{0x3755, 0x00},
	{0x3758, 0x00},
	{0x375b, 0x13},
	{0x3772, 0x23},
	{0x3773, 0x05},
	{0x3774, 0x16},
	{0x3775, 0x12},
	{0x3776, 0x08},
	{0x37a8, 0x38},
	{0x37b5, 0x36},
	{0x37c2, 0x04},
	{0x37c5, 0x00},
	{0x37c7, 0x38},
	{0x37c8, 0x00},
	{0x37d1, 0x13},
	{0x3800, 0x00},
	{0x3801, 0x04},
	{0x3802, 0x00},
	{0x3803, 0x04},
	{0x3804, 0x05},
	{0x3805, 0x0f},
	{0x3806, 0x03},
	{0x3807, 0xcb},
	{0x3808, 0x05},
	{0x3809, 0x04},
	{0x380a, 0x02},
	{0x380b, 0xd4},

	{0x380c, 0x05},
	{0x380d, 0xc6},	
		
	#if 0 /*30fps*/
	{0x380e, 0x04},
	{0x380f, 0x36},
	#else  /*25fps*/
	{0x380e, 0x05},
	{0x380f, 0x10},
	#endif

	{0x3810, 0x00},
	{0x3811, 0x04},
	{0x3812, 0x00},
	{0x3813, 0x04},
	{0x3814, 0x01},
	{0x3815, 0x01},
	{0x3816, 0x00},
	{0x3817, 0x00},
	{0x3818, 0x00},
	{0x3819, 0x00},
	#if SENSOR_FLIP
	{0x3820, 0x86},
	{0x3821, 0x46},
	#else
	{0x3820, 0x80},
	{0x3821, 0x40},
	#endif

	{0x3826, 0x00},
	{0x3827, 0x08},
	{0x382a, 0x01},
	{0x382b, 0x01},
	{0x3836, 0x02},
	{0x3838, 0x10},
	{0x3861, 0x00},
	{0x3862, 0x00},
	{0x3863, 0x02},
	{0x3b00, 0x00},
	{0x3c00, 0x89},
	{0x3c01, 0xab},
	{0x3c02, 0x01},
	{0x3c03, 0x00},
	{0x3c04, 0x00},
	{0x3c05, 0x03},
	{0x3c06, 0x00},
	{0x3c07, 0x05},
	{0x3c0c, 0x00},
	{0x3c0d, 0x00},
	{0x3c0e, 0x00},
	{0x3c0f, 0x00},
	{0x3c40, 0x00},
	{0x3c41, 0xa3},
	{0x3c43, 0x7d},
	{0x3c56, 0x80},
	{0x3c80, 0x08},
	{0x3c82, 0x01},
	{0x3c83, 0x61},
	{0x3d85, 0x17},
	{0x3f08, 0x08},
	{0x3f0a, 0x00},
	{0x3f0b, 0x30},
	{0x4000, 0xcd},
	{0x4003, 0x20},/*BLC*/
	{0x4009, 0x0d},
	{0x4010, 0xf0},
	{0x4011, 0x70},
	{0x4017, 0x10},
	{0x4040, 0x00},
	{0x4041, 0x00},
	{0x4303, 0x00},
	{0x4307, 0x30},
	{0x4500, 0x30},
	{0x4502, 0x40},
	{0x4503, 0x06},
	{0x4508, 0xaa},
	{0x450b, 0x00},
	{0x450c, 0x00},
	{0x4600, 0x00},
	{0x4601, 0x80},
	{0x4700, 0x04},
	{0x4704, 0x00},
	{0x4705, 0x04},
	{0x4837, 0x14},
	{0x484a, 0x3f},
	{0x5000, 0x10},
	{0x5001, 0x01},
	{0x5002, 0x28},
	{0x5004, 0x0c},
	{0x5006, 0x0c},
	{0x5007, 0xe0},
	{0x5008, 0x01},
	{0x5009, 0xb0},
	{0x502a, 0x18},
	{0x5901, 0x00},
	{0x5a01, 0x00},
	{0x5a03, 0x00},
	{0x5a04, 0x0c},
	{0x5a05, 0xe0},
	{0x5a06, 0x09},
	{0x5a07, 0xb0},
	{0x5a08, 0x06},
	{0x5e00, 0x00},
	{0x5e10, 0xfc},
	{0x300f, 0x00},
	{0x3733, 0x10},
	{0x3610, 0xe8},
	{0x3611, 0x56},
	{0x3635, 0x14},
	{0x3636, 0x13},
	{0x3620, 0x84},
	{0x3614, 0x96},
	{0x481f, 0x30},
	{0x3788, 0x00},
	{0x3789, 0x04},
	{0x378a, 0x01},
	{0x378b, 0x60},
	{0x3799, 0x27},
	{0x0100, 0x01},

	{0xFFFF, 0xFF},

};

static const int ov9750_mipi_25fps_exp_time_gain_50Hz[OV9750_25FPS_50HZ_EXP_TIME_TOTAL][3] =
{
	 // {time, analog gain, digital gain}
{10	  ,(int)(	1.00 *128), (int)(1.00*64)},
{10	  ,(int)(	1.00 *128), (int)(1.00*64)},
{10	  ,(int)(	1.00 *128), (int)(1.00*64)},
{11	  ,(int)(	1.00 *128), (int)(1.00*64)},
{11	  ,(int)(	1.00 *128), (int)(1.00*64)},
{12	  ,(int)(	1.00 *128), (int)(1.00*64)},
{12	  ,(int)(	1.00 *128), (int)(1.00*64)},
{12	  ,(int)(	1.00 *128), (int)(1.00*64)},
{13	  ,(int)(	1.00 *128), (int)(1.00*64)},
{13	  ,(int)(	1.00 *128), (int)(1.00*64)},
{14	  ,(int)(	1.00 *128), (int)(1.00*64)},
{14	  ,(int)(	1.00 *128), (int)(1.00*64)},
{15	  ,(int)(	1.00 *128), (int)(1.00*64)},
{15	  ,(int)(	1.00 *128), (int)(1.00*64)},
{16	  ,(int)(	1.00 *128), (int)(1.00*64)},
{16	  ,(int)(	1.00 *128), (int)(1.00*64)},
{17	  ,(int)(	1.00 *128), (int)(1.00*64)},
{18	  ,(int)(	1.00 *128), (int)(1.00*64)},
{18	  ,(int)(	1.00 *128), (int)(1.00*64)},
{19	  ,(int)(	1.00 *128), (int)(1.00*64)},
{20	  ,(int)(	1.00 *128), (int)(1.00*64)},
{20	  ,(int)(	1.00 *128), (int)(1.00*64)},
{21	  ,(int)(	1.00 *128), (int)(1.00*64)},
{22	  ,(int)(	1.00 *128), (int)(1.00*64)},
{22	  ,(int)(	1.00 *128), (int)(1.00*64)},
{23	  ,(int)(	1.00 *128), (int)(1.00*64)},
{24	  ,(int)(	1.00 *128), (int)(1.00*64)},
{25	  ,(int)(	1.00 *128), (int)(1.00*64)},
{26	  ,(int)(	1.00 *128), (int)(1.00*64)},
{27	  ,(int)(	1.00 *128), (int)(1.00*64)},
{28	  ,(int)(	1.00 *128), (int)(1.00*64)},
{29	  ,(int)(	1.00 *128), (int)(1.00*64)},
{30	  ,(int)(	1.00 *128), (int)(1.00*64)},
{31	  ,(int)(	1.00 *128), (int)(1.00*64)},
{32	  ,(int)(	1.00 *128), (int)(1.00*64)},
{33	  ,(int)(	1.00 *128), (int)(1.00*64)},
{34	  ,(int)(	1.00 *128), (int)(1.00*64)},
{35	  ,(int)(	1.00 *128), (int)(1.00*64)},
{37	  ,(int)(	1.00 *128), (int)(1.00*64)},
{38	  ,(int)(	1.00 *128), (int)(1.00*64)},
{39	  ,(int)(	1.00 *128), (int)(1.00*64)},
{41	  ,(int)(	1.00 *128), (int)(1.00*64)},
{42	  ,(int)(	1.00 *128), (int)(1.00*64)},
{43	  ,(int)(	1.00 *128), (int)(1.00*64)},
{45	  ,(int)(	1.00 *128), (int)(1.00*64)},
{47	  ,(int)(	1.00 *128), (int)(1.00*64)},
{48	  ,(int)(	1.00 *128), (int)(1.00*64)},
{50	  ,(int)(	1.00 *128), (int)(1.00*64)},
{52	  ,(int)(	1.00 *128), (int)(1.00*64)},
{53	  ,(int)(	1.00 *128), (int)(1.00*64)},
{55	  ,(int)(	1.00 *128), (int)(1.00*64)},
{57	  ,(int)(	1.00 *128), (int)(1.00*64)},
{59	  ,(int)(	1.00 *128), (int)(1.00*64)},
{61	  ,(int)(	1.00 *128), (int)(1.00*64)},
{64	  ,(int)(	1.00 *128), (int)(1.00*64)},
{66	  ,(int)(	1.00 *128), (int)(1.00*64)},
{68	  ,(int)(	1.00 *128), (int)(1.00*64)},
{71	  ,(int)(	1.00 *128), (int)(1.00*64)},
{73	  ,(int)(	1.00 *128), (int)(1.00*64)},
{76	  ,(int)(	1.00 *128), (int)(1.00*64)},
{78	  ,(int)(	1.00 *128), (int)(1.00*64)},
{81	  ,(int)(	1.00 *128), (int)(1.00*64)},
{84	  ,(int)(	1.00 *128), (int)(1.00*64)},
{87	  ,(int)(	1.00 *128), (int)(1.00*64)},
{90	  ,(int)(	1.00 *128), (int)(1.00*64)},
{93	  ,(int)(	1.00 *128), (int)(1.00*64)},
{96	  ,(int)(	1.00 *128), (int)(1.00*64)},
{100	,(int)(	1.00 *128), (int)(1.00*64)},
{103	,(int)(	1.00 *128), (int)(1.00*64)},
{107	,(int)(	1.00 *128), (int)(1.00*64)},
{111	,(int)(	1.00 *128), (int)(1.00*64)},
{115	,(int)(	1.00 *128), (int)(1.00*64)},
{119	,(int)(	1.00 *128), (int)(1.00*64)},
{123	,(int)(	1.00 *128), (int)(1.00*64)},
{127	,(int)(	1.00 *128), (int)(1.00*64)},
{132	,(int)(	1.00 *128), (int)(1.00*64)},
{136	,(int)(	1.00 *128), (int)(1.00*64)},
{141	,(int)(	1.00 *128), (int)(1.00*64)},
{146	,(int)(	1.00 *128), (int)(1.00*64)},
{151	,(int)(	1.00 *128), (int)(1.00*64)},
{156	,(int)(	1.00 *128), (int)(1.00*64)},
{162	,(int)(	1.00 *128), (int)(1.00*64)},
{168	,(int)(	1.00 *128), (int)(1.00*64)},
{174	,(int)(	1.00 *128), (int)(1.00*64)},
{180	,(int)(	1.00 *128), (int)(1.00*64)},
{186	,(int)(	1.00 *128), (int)(1.00*64)},
{193	,(int)(	1.00 *128), (int)(1.00*64)},
{199	,(int)(	1.00 *128), (int)(1.00*64)},
{206	,(int)(	1.00 *128), (int)(1.00*64)},
{214	,(int)(	1.00 *128), (int)(1.00*64)},
{221	,(int)(	1.00 *128), (int)(1.00*64)},
{229	,(int)(	1.00 *128), (int)(1.00*64)},
{237	,(int)(	1.00 *128), (int)(1.00*64)},
{246	,(int)(	1.00 *128), (int)(1.00*64)},
{254	,(int)(	1.00 *128), (int)(1.00*64)},
{263	,(int)(	1.00 *128), (int)(1.00*64)},
{272	,(int)(	1.00 *128), (int)(1.00*64)},
{282	,(int)(	1.00 *128), (int)(1.00*64)},
{292	,(int)(	1.00 *128), (int)(1.00*64)},
{302	,(int)(	1.00 *128), (int)(1.00*64)},
{313	,(int)(	1.00 *128), (int)(1.00*64)},
{324	,(int)(	1.00 *128), (int)(1.00*64)},
{324	,(int)(	1.04 *128), (int)(1.00*64)},
{324	,(int)(	1.07 *128), (int)(1.00*64)},
{324	,(int)(	1.11 *128), (int)(1.00*64)},
{324	,(int)(	1.15 *128), (int)(1.00*64)},
{324	,(int)(	1.19 *128), (int)(1.00*64)},
{324	,(int)(	1.23 *128), (int)(1.00*64)},
{324	,(int)(	1.27 *128), (int)(1.00*64)},
{324	,(int)(	1.32 *128), (int)(1.00*64)},
{324	,(int)(	1.37 *128), (int)(1.00*64)},
{324	,(int)(	1.41 *128), (int)(1.00*64)},
{324	,(int)(	1.46 *128), (int)(1.00*64)},
{324	,(int)(	1.52 *128), (int)(1.00*64)},
{324	,(int)(	1.57 *128), (int)(1.00*64)},
{324	,(int)(	1.62 *128), (int)(1.00*64)},
{324	,(int)(	1.68 *128), (int)(1.00*64)},
{324	,(int)(	1.74 *128), (int)(1.00*64)},
{324	,(int)(	1.80 *128), (int)(1.00*64)},
{324	,(int)(	1.87 *128), (int)(1.00*64)},
{324	,(int)(	1.93 *128), (int)(1.00*64)},
{648	,(int)(	1.00 *128), (int)(1.00*64)},
{648	,(int)(	1.04 *128), (int)(1.00*64)},
{648	,(int)(	1.07 *128), (int)(1.00*64)},
{648	,(int)(	1.11 *128), (int)(1.00*64)},
{648	,(int)(	1.15 *128), (int)(1.00*64)},
{648	,(int)(	1.19 *128), (int)(1.00*64)},
{648	,(int)(	1.23 *128), (int)(1.00*64)},
{648	,(int)(	1.27 *128), (int)(1.00*64)},
{648	,(int)(	1.32 *128), (int)(1.00*64)},
{648	,(int)(	1.37 *128), (int)(1.00*64)},
{648	,(int)(	1.41 *128), (int)(1.00*64)},
{648	,(int)(	1.46 *128), (int)(1.00*64)},
{648	,(int)(	1.52 *128), (int)(1.00*64)},
{648	,(int)(	1.57 *128), (int)(1.00*64)},
{648	,(int)(	1.62 *128), (int)(1.00*64)},
{648	,(int)(	1.68 *128), (int)(1.00*64)},
{648	,(int)(	1.74 *128), (int)(1.00*64)},
{648	,(int)(	1.80 *128), (int)(1.00*64)},
{648	,(int)(	1.87 *128), (int)(1.00*64)},
{648	,(int)(	1.93 *128), (int)(1.00*64)},
{972	,(int)(	1.32 *128), (int)(1.00*64)},
{972	,(int)(	1.37 *128), (int)(1.00*64)},
{972	,(int)(	1.41 *128), (int)(1.00*64)},
{972	,(int)(	1.46 *128), (int)(1.00*64)},
{972	,(int)(	1.52 *128), (int)(1.00*64)},
{972	,(int)(	1.57 *128), (int)(1.00*64)},
{972	,(int)(	1.62 *128), (int)(1.00*64)},
{972	,(int)(	1.68 *128), (int)(1.00*64)},
{972	,(int)(	1.74 *128), (int)(1.00*64)},
{972	,(int)(	1.80 *128), (int)(1.00*64)},
{972	,(int)(	1.87 *128), (int)(1.00*64)},
{972	,(int)(	1.93 *128), (int)(1.00*64)},
{1296	,(int)(	1.46 *128), (int)(1.00*64)},
{1296	,(int)(	1.52 *128), (int)(1.00*64)},
{1296	,(int)(	1.57 *128), (int)(1.00*64)},
{1296	,(int)(	1.62 *128), (int)(1.00*64)},
{1296	,(int)(	1.68 *128), (int)(1.00*64)},
{1296	,(int)(	1.74 *128), (int)(1.00*64)},
{1296	,(int)(	1.80 *128), (int)(1.00*64)},
{1296	,(int)(	1.87 *128), (int)(1.00*64)},
{1296	,(int)(	1.93 *128), (int)(1.00*64)},
{1296	,(int)(	2.00 *128), (int)(1.00*64)},
{1296	,(int)(	2.07 *128), (int)(1.00*64)},
{1296	,(int)(	2.14 *128), (int)(1.00*64)},
{1296	,(int)(	2.22 *128), (int)(1.00*64)},
{1296	,(int)(	2.30 *128), (int)(1.00*64)},
{1296	,(int)(	2.38 *128), (int)(1.00*64)},
{1296	,(int)(	2.46 *128), (int)(1.00*64)},
{1296	,(int)(	2.55 *128), (int)(1.00*64)},
{1296	,(int)(	2.64 *128), (int)(1.00*64)},
{1296	,(int)(	2.73 *128), (int)(1.00*64)},
{1296	,(int)(	2.83 *128), (int)(1.00*64)},
{1296	,(int)(	2.93 *128), (int)(1.00*64)},
{1296	,(int)(	3.03 *128), (int)(1.00*64)},
{1296	,(int)(	3.14 *128), (int)(1.00*64)},
{1296	,(int)(	3.25 *128), (int)(1.00*64)},
{1296	,(int)(	3.36 *128), (int)(1.00*64)},
{1296	,(int)(	3.48 *128), (int)(1.00*64)},
{1296	,(int)(	3.61 *128), (int)(1.00*64)},
{1296	,(int)(	3.73 *128), (int)(1.00*64)},
{1296	,(int)(	3.86 *128), (int)(1.00*64)},
{1296	,(int)(	4.00 *128), (int)(1.00*64)},
{1296	,(int)(	4.14 *128), (int)(1.00*64)},
{1296	,(int)(	4.29 *128), (int)(1.00*64)},
{1296	,(int)(	4.44 *128), (int)(1.00*64)},
{1296	,(int)(	4.59 *128), (int)(1.00*64)},
{1296	,(int)(	4.76 *128), (int)(1.00*64)},
{1296	,(int)(	4.92 *128), (int)(1.00*64)},
{1296	,(int)(	5.10 *128), (int)(1.00*64)},
{1296	,(int)(	5.28 *128), (int)(1.00*64)},
{1296	,(int)(	5.46 *128), (int)(1.00*64)},
{1296	,(int)(	5.66 *128), (int)(1.00*64)},
{1296	,(int)(	5.86 *128), (int)(1.00*64)},
{1296	,(int)(	6.06 *128), (int)(1.00*64)},
{1296	,(int)(	6.28 *128), (int)(1.00*64)},
{1296	,(int)(	6.50 *128), (int)(1.00*64)},
{1296	,(int)(	6.73 *128), (int)(1.00*64)},
{1296	,(int)(	6.96 *128), (int)(1.00*64)},
{1296	,(int)(	7.21 *128), (int)(1.00*64)},
{1296	,(int)(	7.46 *128), (int)(1.00*64)},
{1296	,(int)(	7.73 *128), (int)(1.00*64)},
{1296	,(int)(	8.00 *128), (int)(1.00*64)},
{1296	,(int)(	8.28 *128), (int)(1.00*64)},
{1296	,(int)(	8.57 *128), (int)(1.00*64)},
{1296	,(int)(	8.88 *128), (int)(1.00*64)},
{1296	,(int)(	9.19 *128), (int)(1.00*64)},
{1296	,(int)(	9.51 *128), (int)(1.00*64)},
{1296	,(int)(	9.85 *128), (int)(1.00*64)},
{1296	,(int)( 10.20*128), (int)(1.00*64)},
{1296	,(int)( 10.56*128), (int)(1.00*64)},
{1296	,(int)( 10.93*128), (int)(1.00*64)},
{1296	,(int)( 11.31*128), (int)(1.00*64)},
{1296	,(int)( 11.71*128), (int)(1.00*64)},
{1296	,(int)( 12.13*128), (int)(1.00*64)},
{1296	,(int)( 12.55*128), (int)(1.00*64)},
{1296	,(int)( 13.00*128), (int)(1.00*64)},
{1296	,(int)( 13.45*128), (int)(1.00*64)},
{1296	,(int)( 13.93*128), (int)(1.00*64)},
{1296	,(int)( 14.42*128), (int)(1.00*64)},
{1296	,(int)( 14.93*128), (int)(1.00*64)},
{1296	,(int)( 15.45*128), (int)(1.00*64)},
{1296	,(int)( 16.00*128), (int)(1.00*64)},
{1296	,(int)( 16.56*128), (int)(1.00*64)},
{1296	,(int)( 17.15*128), (int)(1.00*64)},
{1296	,(int)( 17.75*128), (int)(1.00*64)},
{1296	,(int)( 18.38*128), (int)(1.00*64)},
{1296	,(int)( 19.03*128), (int)(1.00*64)},
{1296	,(int)( 19.70*128), (int)(1.00*64)},
{1296	,(int)( 20.39*128), (int)(1.00*64)},
{1296	,(int)( 21.11*128), (int)(1.00*64)},
{1296	,(int)( 21.86*128), (int)(1.00*64)},
{1296	,(int)( 22.63*128), (int)(1.00*64)},
{1296	,(int)( 23.43*128), (int)(1.00*64)},
{1296	,(int)( 24.25*128), (int)(1.00*64)},
{1296	,(int)( 25.11*128), (int)(1.00*64)},
{1296	,(int)( 25.99*128), (int)(1.00*64)},
{1296	,(int)( 26.91*128), (int)(1.00*64)},
{1296	,(int)( 27.86*128), (int)(1.00*64)},
{1296	,(int)( 28.84*128), (int)(1.00*64)},
{1296	,(int)( 29.86*128), (int)(1.00*64)},
{1296	,(int)( 30.91*128), (int)(1.00*64)},
{1296	,(int)( 32.00*128), (int)(1.00*64)},
/*
{1296	,(int)( 33.13*128), (int)(1.00*64)},
{1296	,(int)( 34.30*128), (int)(1.00*64)},
{1296	,(int)( 35.51*128), (int)(1.00*64)},
{1296	,(int)( 36.76*128), (int)(1.00*64)},
{1296	,(int)( 38.05*128), (int)(1.00*64)},
{1296	,(int)( 39.40*128), (int)(1.00*64)},
{1296	,(int)( 40.79*128), (int)(1.00*64)},
{1296	,(int)( 42.22*128), (int)(1.00*64)},
{1296	,(int)( 43.71*128), (int)(1.00*64)},
{1296	,(int)( 45.25*128), (int)(1.00*64)},
{1296	,(int)( 46.85*128), (int)(1.00*64)},
{1296	,(int)( 48.50*128), (int)(1.00*64)},
{1296	,(int)( 50.21*128), (int)(1.00*64)},
{1296	,(int)( 51.98*128), (int)(1.00*64)},
{1296	,(int)( 53.82*128), (int)(1.00*64)},
{1296	,(int)( 55.72*128), (int)(1.00*64)},
{1296	,(int)( 57.68*128), (int)(1.00*64)},
{1296	,(int)( 59.71*128), (int)(1.00*64)},
{1296	,(int)( 61.82*128), (int)(1.00*64)}*/

};

static const int ov9750_mipi_25fps_exp_time_gain_60Hz[OV9750_25FPS_60HZ_EXP_TIME_TOTAL][3] =
{
	 // {time, analog gain, digital gain}
{10	  ,(int)( 1.00*128), (int)(1.00*64)},
{10	  ,(int)( 1.00*128), (int)(1.00*64)},
{10	  ,(int)( 1.00*128), (int)(1.00*64)},
{11	  ,(int)( 1.00*128), (int)(1.00*64)},
{11	  ,(int)( 1.00*128), (int)(1.00*64)},
{12	  ,(int)( 1.00*128), (int)(1.00*64)},
{12	  ,(int)( 1.00*128), (int)(1.00*64)},
{12	  ,(int)( 1.00*128), (int)(1.00*64)},
{13	  ,(int)( 1.00*128), (int)(1.00*64)},
{13	  ,(int)( 1.00*128), (int)(1.00*64)},
{14	  ,(int)( 1.00*128), (int)(1.00*64)},
{14	  ,(int)( 1.00*128), (int)(1.00*64)},
{15	  ,(int)( 1.00*128), (int)(1.00*64)},
{15	  ,(int)( 1.00*128), (int)(1.00*64)},
{16	  ,(int)( 1.00*128), (int)(1.00*64)},
{16	  ,(int)( 1.00*128), (int)(1.00*64)},
{17	  ,(int)( 1.00*128), (int)(1.00*64)},
{17	  ,(int)( 1.00*128), (int)(1.00*64)},
{18	  ,(int)( 1.00*128), (int)(1.00*64)},
{19	  ,(int)( 1.00*128), (int)(1.00*64)},
{19	  ,(int)( 1.00*128), (int)(1.00*64)},
{20	  ,(int)( 1.00*128), (int)(1.00*64)},
{21	  ,(int)( 1.00*128), (int)(1.00*64)},
{22	  ,(int)( 1.00*128), (int)(1.00*64)},
{22	  ,(int)( 1.00*128), (int)(1.00*64)},
{23	  ,(int)( 1.00*128), (int)(1.00*64)},
{24	  ,(int)( 1.00*128), (int)(1.00*64)},
{25	  ,(int)( 1.00*128), (int)(1.00*64)},
{26	  ,(int)( 1.00*128), (int)(1.00*64)},
{26	  ,(int)( 1.00*128), (int)(1.00*64)},
{27	  ,(int)( 1.00*128), (int)(1.00*64)},
{28	  ,(int)( 1.00*128), (int)(1.00*64)},
{29	  ,(int)( 1.00*128), (int)(1.00*64)},
{30	  ,(int)( 1.00*128), (int)(1.00*64)},
{31	  ,(int)( 1.00*128), (int)(1.00*64)},
{33	  ,(int)( 1.00*128), (int)(1.00*64)},
{34	  ,(int)( 1.00*128), (int)(1.00*64)},
{35	  ,(int)( 1.00*128), (int)(1.00*64)},
{36	  ,(int)( 1.00*128), (int)(1.00*64)},
{37	  ,(int)( 1.00*128), (int)(1.00*64)},
{39	  ,(int)( 1.00*128), (int)(1.00*64)},
{40	  ,(int)( 1.00*128), (int)(1.00*64)},
{42	  ,(int)( 1.00*128), (int)(1.00*64)},
{43	  ,(int)( 1.00*128), (int)(1.00*64)},
{45	  ,(int)( 1.00*128), (int)(1.00*64)},
{46	  ,(int)( 1.00*128), (int)(1.00*64)},
{48	  ,(int)( 1.00*128), (int)(1.00*64)},
{49	  ,(int)( 1.00*128), (int)(1.00*64)},
{51	  ,(int)( 1.00*128), (int)(1.00*64)},
{53	  ,(int)( 1.00*128), (int)(1.00*64)},
{55	  ,(int)( 1.00*128), (int)(1.00*64)},
{57	  ,(int)( 1.00*128), (int)(1.00*64)},
{59	  ,(int)( 1.00*128), (int)(1.00*64)},
{61	  ,(int)( 1.00*128), (int)(1.00*64)},
{63	  ,(int)( 1.00*128), (int)(1.00*64)},
{65	  ,(int)( 1.00*128), (int)(1.00*64)},
{67	  ,(int)( 1.00*128), (int)(1.00*64)},
{70	  ,(int)( 1.00*128), (int)(1.00*64)},
{72	  ,(int)( 1.00*128), (int)(1.00*64)},
{75	  ,(int)( 1.00*128), (int)(1.00*64)},
{78	  ,(int)( 1.00*128), (int)(1.00*64)},
{80	  ,(int)( 1.00*128), (int)(1.00*64)},
{83	  ,(int)( 1.00*128), (int)(1.00*64)},
{86	  ,(int)( 1.00*128), (int)(1.00*64)},
{89	  ,(int)( 1.00*128), (int)(1.00*64)},
{92	  ,(int)( 1.00*128), (int)(1.00*64)},
{95	  ,(int)( 1.00*128), (int)(1.00*64)},
{99	  ,(int)( 1.00*128), (int)(1.00*64)},
{102	,(int)( 1.00*128), (int)(1.00*64)},
{106	,(int)( 1.00*128), (int)(1.00*64)},
{110	,(int)( 1.00*128), (int)(1.00*64)},
{114	,(int)( 1.00*128), (int)(1.00*64)},
{118	,(int)( 1.00*128), (int)(1.00*64)},
{122	,(int)( 1.00*128), (int)(1.00*64)},
{126	,(int)( 1.00*128), (int)(1.00*64)},
{130	,(int)( 1.00*128), (int)(1.00*64)},
{135	,(int)( 1.00*128), (int)(1.00*64)},
{140	,(int)( 1.00*128), (int)(1.00*64)},
{145	,(int)( 1.00*128), (int)(1.00*64)},
{150	,(int)( 1.00*128), (int)(1.00*64)},
{155	,(int)( 1.00*128), (int)(1.00*64)},
{161	,(int)( 1.00*128), (int)(1.00*64)},
{166	,(int)( 1.00*128), (int)(1.00*64)},
{172	,(int)( 1.00*128), (int)(1.00*64)},
{178	,(int)( 1.00*128), (int)(1.00*64)},
{184	,(int)( 1.00*128), (int)(1.00*64)},
{191	,(int)( 1.00*128), (int)(1.00*64)},
{198	,(int)( 1.00*128), (int)(1.00*64)},
{205	,(int)( 1.00*128), (int)(1.00*64)},
{212	,(int)( 1.00*128), (int)(1.00*64)},
{219	,(int)( 1.00*128), (int)(1.00*64)},
{227	,(int)( 1.00*128), (int)(1.00*64)},
{235	,(int)( 1.00*128), (int)(1.00*64)},
{243	,(int)( 1.00*128), (int)(1.00*64)},
{252	,(int)( 1.00*128), (int)(1.00*64)},
{261	,(int)( 1.00*128), (int)(1.00*64)},
{270	,(int)( 1.00*128), (int)(1.00*64)},
{270	,(int)( 1.04*128), (int)(1.00*64)},
{270	,(int)( 1.07*128), (int)(1.00*64)},
{270	,(int)( 1.11*128), (int)(1.00*64)},
{270	,(int)( 1.15*128), (int)(1.00*64)},
{270	,(int)( 1.19*128), (int)(1.00*64)},
{270	,(int)( 1.23*128), (int)(1.00*64)},
{270	,(int)( 1.27*128), (int)(1.00*64)},
{270	,(int)( 1.32*128), (int)(1.00*64)},
{270	,(int)( 1.37*128), (int)(1.00*64)},
{270	,(int)( 1.41*128), (int)(1.00*64)},
{270	,(int)( 1.46*128), (int)(1.00*64)},
{270	,(int)( 1.52*128), (int)(1.00*64)},
{270	,(int)( 1.57*128), (int)(1.00*64)},
{270	,(int)( 1.62*128), (int)(1.00*64)},
{270	,(int)( 1.68*128), (int)(1.00*64)},
{270	,(int)( 1.74*128), (int)(1.00*64)},
{270	,(int)( 1.80*128), (int)(1.00*64)},
{270	,(int)( 1.87*128), (int)(1.00*64)},
{270	,(int)( 1.93*128), (int)(1.00*64)},
{540	,(int)( 1.00*128), (int)(1.00*64)},
{540	,(int)( 1.04*128), (int)(1.00*64)},
{540	,(int)( 1.07*128), (int)(1.00*64)},
{540	,(int)( 1.11*128), (int)(1.00*64)},
{540	,(int)( 1.15*128), (int)(1.00*64)},
{540	,(int)( 1.19*128), (int)(1.00*64)},
{540	,(int)( 1.23*128), (int)(1.00*64)},
{540	,(int)( 1.27*128), (int)(1.00*64)},
{540	,(int)( 1.32*128), (int)(1.00*64)},
{540	,(int)( 1.37*128), (int)(1.00*64)},
{540	,(int)( 1.41*128), (int)(1.00*64)},
{540	,(int)( 1.46*128), (int)(1.00*64)},
{540	,(int)( 1.52*128), (int)(1.00*64)},
{540	,(int)( 1.57*128), (int)(1.00*64)},
{540	,(int)( 1.62*128), (int)(1.00*64)},
{540	,(int)( 1.68*128), (int)(1.00*64)},
{540	,(int)( 1.74*128), (int)(1.00*64)},
{540	,(int)( 1.80*128), (int)(1.00*64)},
{540	,(int)( 1.87*128), (int)(1.00*64)},
{540	,(int)( 1.93*128), (int)(1.00*64)},
{810	,(int)( 1.32*128), (int)(1.00*64)},
{810	,(int)( 1.37*128), (int)(1.00*64)},
{810	,(int)( 1.41*128), (int)(1.00*64)},
{810	,(int)( 1.46*128), (int)(1.00*64)},
{810	,(int)( 1.52*128), (int)(1.00*64)},
{810	,(int)( 1.57*128), (int)(1.00*64)},
{810	,(int)( 1.62*128), (int)(1.00*64)},
{810	,(int)( 1.68*128), (int)(1.00*64)},
{810	,(int)( 1.74*128), (int)(1.00*64)},
{810	,(int)( 1.80*128), (int)(1.00*64)},
{810	,(int)( 1.87*128), (int)(1.00*64)},
{810	,(int)( 1.93*128), (int)(1.00*64)},
{1080	,(int)( 1.46*128), (int)(1.00*64)},
{1080	,(int)( 1.52*128), (int)(1.00*64)},
{1080	,(int)( 1.57*128), (int)(1.00*64)},
{1080	,(int)( 1.62*128), (int)(1.00*64)},
{1080	,(int)( 1.68*128), (int)(1.00*64)},
{1080	,(int)( 1.74*128), (int)(1.00*64)},
{1080	,(int)( 1.80*128), (int)(1.00*64)},
{1080	,(int)( 1.87*128), (int)(1.00*64)},
{1080	,(int)( 1.93*128), (int)(1.00*64)},
{1080	,(int)( 2.00*128), (int)(1.00*64)},
{1080	,(int)( 2.07*128), (int)(1.00*64)},
{1080	,(int)( 2.14*128), (int)(1.00*64)},
{1080	,(int)( 2.22*128), (int)(1.00*64)},
{1080	,(int)( 2.30*128), (int)(1.00*64)},
{1080	,(int)( 2.38*128), (int)(1.00*64)},
{1080	,(int)( 2.46*128), (int)(1.00*64)},
{1080	,(int)( 2.55*128), (int)(1.00*64)},
{1080	,(int)( 2.64*128), (int)(1.00*64)},
{1080	,(int)( 2.73*128), (int)(1.00*64)},
{1080	,(int)( 2.83*128), (int)(1.00*64)},
{1080	,(int)( 2.93*128), (int)(1.00*64)},
{1080	,(int)( 3.03*128), (int)(1.00*64)},
{1080	,(int)( 3.14*128), (int)(1.00*64)},
{1080	,(int)( 3.25*128), (int)(1.00*64)},
{1080	,(int)( 3.36*128), (int)(1.00*64)},
{1080	,(int)( 3.48*128), (int)(1.00*64)},
{1080	,(int)( 3.61*128), (int)(1.00*64)},
{1080	,(int)( 3.73*128), (int)(1.00*64)},
{1080	,(int)( 3.86*128), (int)(1.00*64)},
{1080	,(int)( 4.00*128), (int)(1.00*64)},
{1080	,(int)( 4.14*128), (int)(1.00*64)},
{1080	,(int)( 4.29*128), (int)(1.00*64)},
{1080	,(int)( 4.44*128), (int)(1.00*64)},
{1080	,(int)( 4.59*128), (int)(1.00*64)},
{1080	,(int)( 4.76*128), (int)(1.00*64)},
{1080	,(int)( 4.92*128), (int)(1.00*64)},
{1080	,(int)( 5.10*128), (int)(1.00*64)},
{1080	,(int)( 5.28*128), (int)(1.00*64)},
{1080	,(int)( 5.46*128), (int)(1.00*64)},
{1080	,(int)( 5.66*128), (int)(1.00*64)},
{1080	,(int)( 5.86*128), (int)(1.00*64)},
{1080	,(int)( 6.06*128), (int)(1.00*64)},
{1080	,(int)( 6.28*128), (int)(1.00*64)},
{1080	,(int)( 6.50*128), (int)(1.00*64)},
{1080	,(int)( 6.73*128), (int)(1.00*64)},
{1080	,(int)( 6.96*128), (int)(1.00*64)},
{1080	,(int)( 7.21*128), (int)(1.00*64)},
{1080	,(int)( 7.46*128), (int)(1.00*64)},
{1080	,(int)( 7.73*128), (int)(1.00*64)},
{1080	,(int)( 8.00*128), (int)(1.00*64)},
{1080	,(int)( 8.28*128), (int)(1.00*64)},
{1080	,(int)( 8.57*128), (int)(1.00*64)},
{1080	,(int)( 8.88*128), (int)(1.00*64)},
{1080	,(int)( 9.19*128), (int)(1.00*64)},
{1080	,(int)( 9.51*128), (int)(1.00*64)},
{1080	,(int)( 9.85*128), (int)(1.00*64)},
{1080 ,(int)(10.20*128), (int)(1.00*64)},
{1080 ,(int)(10.56*128), (int)(1.00*64)},
{1080 ,(int)(10.93*128), (int)(1.00*64)},
{1080 ,(int)(11.31*128), (int)(1.00*64)},
{1080 ,(int)(11.71*128), (int)(1.00*64)},
{1080 ,(int)(12.13*128), (int)(1.00*64)},
{1080 ,(int)(12.55*128), (int)(1.00*64)},
{1080 ,(int)(13.00*128), (int)(1.00*64)},
{1080 ,(int)(13.45*128), (int)(1.00*64)},
{1080 ,(int)(13.93*128), (int)(1.00*64)},
{1080 ,(int)(14.42*128), (int)(1.00*64)},
{1080 ,(int)(14.93*128), (int)(1.00*64)},
{1080 ,(int)(15.45*128), (int)(1.00*64)},
{1080 ,(int)(16.00*128), (int)(1.00*64)},
{1080 ,(int)(16.56*128), (int)(1.00*64)},
{1080 ,(int)(17.15*128), (int)(1.00*64)},
{1080 ,(int)(17.75*128), (int)(1.00*64)},
{1080 ,(int)(18.38*128), (int)(1.00*64)},
{1080 ,(int)(19.03*128), (int)(1.00*64)},
{1080 ,(int)(19.70*128), (int)(1.00*64)},
{1080 ,(int)(20.39*128), (int)(1.00*64)},
{1080 ,(int)(21.11*128), (int)(1.00*64)},
{1080 ,(int)(21.86*128), (int)(1.00*64)},
{1080 ,(int)(22.63*128), (int)(1.00*64)},
{1080 ,(int)(23.43*128), (int)(1.00*64)},
{1080 ,(int)(24.25*128), (int)(1.00*64)},
{1080 ,(int)(25.11*128), (int)(1.00*64)},
{1080 ,(int)(25.99*128), (int)(1.00*64)},
{1080 ,(int)(26.91*128), (int)(1.00*64)},
{1080 ,(int)(27.86*128), (int)(1.00*64)},
{1080 ,(int)(28.84*128), (int)(1.00*64)},
{1080 ,(int)(29.86*128), (int)(1.00*64)},
{1080 ,(int)(30.91*128), (int)(1.00*64)},
{1080 ,(int)(32.00*128), (int)(1.00*64)},
/*
{1080 ,(int)(33.13*128), (int)(1.00*64)},
{1080 ,(int)(34.30*128), (int)(1.00*64)},
{1080 ,(int)(35.51*128), (int)(1.00*64)},
{1080 ,(int)(36.76*128), (int)(1.00*64)},
{1080 ,(int)(38.05*128), (int)(1.00*64)},
{1080 ,(int)(39.40*128), (int)(1.00*64)},
{1080 ,(int)(40.79*128), (int)(1.00*64)},
{1080 ,(int)(42.22*128), (int)(1.00*64)},
{1080 ,(int)(43.71*128), (int)(1.00*64)},
{1080 ,(int)(45.25*128), (int)(1.00*64)},
{1080 ,(int)(46.85*128), (int)(1.00*64)},
{1080 ,(int)(48.50*128), (int)(1.00*64)},
{1080 ,(int)(50.21*128), (int)(1.00*64)},
{1080 ,(int)(51.98*128), (int)(1.00*64)},
{1080 ,(int)(53.82*128), (int)(1.00*64)},
{1080 ,(int)(55.72*128), (int)(1.00*64)},
{1080 ,(int)(57.68*128), (int)(1.00*64)},
{1080 ,(int)(59.71*128), (int)(1.00*64)},
{1080 ,(int)(61.82*128), (int)(1.00*64)},*/

};


////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTION IMPLEMENTATION
////////////////////////////////////////////////////////////////////////////////////////////////////////
static INT32S ov9750_sccb_open(void)
{
#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW
	drv_l1_i2c_init(&ov9750_handle);
	//DBG_PRINT("SENSOR_I2C_MODE_HW\n");
#else
	//DBG_PRINT("SENSOR_I2C_MODE_SW\n");
#endif
	return STATUS_OK;
}

static void ov9750_sccb_close(void)
{
#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW
	drv_l1_i2c_uninit(&ov9750_handle);
#endif
}

static INT32S ov9750_sccb_write(INT16U reg, INT8U value)
{
#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW
	return drv_l2_sccb_write(&ov9750_handle, reg, value);
#else
	return drv_l1_reg_2byte_data_1byte_write(&ov9750_handle, reg, value);
#endif
}

static INT32S ov9750_sccb_read(INT16U reg,INT8U *value)
{
#if SENSOR_I2C_MODE == SENSOR_I2C_MODE_SW

	INT16U data;
	if(drv_l2_sccb_read(&ov9750_handle, reg, &data) >= 0){
		//DBG_PRINT("[SW] R:0x%02x, D:0x%02x\n", reg, data);
		*value = data;
		return STATUS_OK;
	}
	else
	{
		DBG_PRINT("ov9750_sccb_read fail!\n");
		
		return STATUS_FAIL;
	}
#else //SENSOR_I2C_MODE == SENSOR_I2C_MODE_HW
	
	if(drv_l1_reg_2byte_data_1byte_read(&ov9750_handle, reg, value) >=0)
	{
		//DBG_PRINT("0x%02x, 0x%02x\r\n", reg, value);
		return STATUS_OK;
	}
	else
	{
		DBG_PRINT("ov9750_sccb_read fail...\r\n");
		return STATUS_FAIL;
	}
	
#endif	
}


static INT32S ov9750_sccb_write_table(regval16_t *pTable)
{

	INT32U retry_cnt = 0;

	while(1)
	{
		drv_l1_sys_wdt_clear();
		if(pTable->reg_num == 0xFFFF && pTable->value == 0xFF){
			break;
		}
		else if(pTable->reg_num == 0xffff && pTable->value == 0xf0){		//I2C_DELAY_FLAG_16
			OSTimeDly(10);
		}
		else if(ov9750_sccb_write(pTable->reg_num, pTable->value) < 0){
			DBG_PRINT("sccb write fail.\n");
			retry_cnt += 1;
			if(retry_cnt >= 3)
				return STATUS_FAIL;
			continue;
		}
		else{
			retry_cnt = 0;
		}

		pTable++;

	}

	return STATUS_OK;
}

static INT32S ov9750_sccb_read_table(regval16_t *pTable)
{
	
	INT8U 	Rd_data;
	while(1) {
		if(pTable->reg_num == 0xFFFF && pTable->value == 0xFF) {
			break;
		}

		if(ov9750_sccb_read(pTable->reg_num,&Rd_data) < 0) {
			DBG_PRINT("sccb read fail.\r\n");
			continue;
		}
		DBG_PRINT("addr=0x%x w_data = 0x%x r_data=0x%x \r\n",pTable->reg_num,pTable->value,Rd_data);
		pTable++;
	}
	DBG_PRINT("\r\n\r\n");

	return STATUS_OK;
}


/*****************************************************************************************
+++++++++++++++++AEC/AGC
*****************************************************************************************/

static int ov9750_cvt_agc_gain(int analog_gain)
{
   //	DBG_PRINT("gain:0x%x,0x%x\r\n",(analog_gain>>8)&0x1f,analog_gain&0xff);
    ov9750_sccb_write(0x3508, (analog_gain>>8)&0x1f );
	ov9750_sccb_write(0x3509, analog_gain&0xff);
}



static int ov9750_set_xfps_exposure_time(sensor_exposure_t *si)
{

	INT8U   lsb_time, msb_time;
	INT32U	idx, gain;
	INT8S	iso_x = 1;


	//DBG_PRINT("%s:%d\n", __FUNCTION__, __LINE__);
	/*get iso val*/
	iso_x = (drv_l2_CdspGetIso() & 0xF);
	if(iso_x <= 0)
		iso_x = 1;

	si->sensor_ev_idx += si->ae_ev_step;
	if(si->sensor_ev_idx >= si->max_ev_idx)
		si->sensor_ev_idx = si->max_ev_idx;
	if(si->sensor_ev_idx < 0)
		si->sensor_ev_idx = 0;


	idx = si->sensor_ev_idx * 3;
	si->time = p_expTime_table[idx];
	si->analog_gain = p_expTime_table[idx + 1];
	si->digital_gain = p_expTime_table[idx+2];
	//iso set
	si->analog_gain = (si->analog_gain * iso_x);

#if 0
	si->time = 1130;
	si->analog_gain = 25*100;
	si->digital_gain = 1*64;

	DBG_PRINT("lum:%d\n",gp_cdsp_ae_get_result_lum_me());
#endif

#if 0
	if(si->time<722 && night_mode != 0){
		night_mode = 0;			

		/*25fps set*/
		OV9750_sccb_write(0x380e, 0x03);
		OV9750_sccb_write(0x380f, 0xC6);	
		OV9750_sccb_write(0x4003 , 0x03);  /*BLC*/		
	}
	else if(si->time>=964 && si->analog_gain>5*64&& night_mode != 1){
		night_mode = 1;	
		
		/*20fps set*/		
		OV9750_sccb_write(0x380e, 0x04);
		OV9750_sccb_write(0x380f, 0xBC);	
		OV9750_sccb_write(0x4003 , 0x06);  /*BLC*/
	}
#endif

	// exposure time
	if(si->time != pre_sensor_time){	    
		pre_sensor_time = si->time;
		lsb_time = (si->time<<4)&0xff;
		msb_time= (si->time>>4)&0xff;			
		ov9750_sccb_write(0x3502, lsb_time);
		ov9750_sccb_write(0x3501, msb_time);
		//DBG_PRINT("time:0x%x,0x%x\r\n",lsb_time,msb_time);
	}

	//gain
	if(si->analog_gain != pre_sensor_a_gain){
		pre_sensor_a_gain = si->analog_gain;
	    ov9750_cvt_agc_gain(si->analog_gain);
	}

	return 0;
}


static void ov9750_set_ae(int ev_step)
{
	seInfo.ae_ev_step = ev_step;
	ov9750_set_xfps_exposure_time(&seInfo);
}

void sensor_register_ae_ctrl(INT32U *handle)
{
	*handle = (INT32U) ov9750_set_ae;
}

void sensor_get_ae_info(sensor_exposure_t *si)
{
	gp_memcpy((INT8S*)si, (INT8S*)&seInfo, sizeof(sensor_exposure_t));
}

void sensor_set_max_lum(int max_lum)
{
	seInfo.max_ev_idx = seInfo.total_ev_idx - (128 - max_lum);
	//DBG_PRINT("MaxLum:%d, MaxEvIdx:%d, TotalEvIdx:%d\n",max_lum, seInfo.max_ev_idx, seInfo.total_ev_idx);
}


void ov9750_mipi_set_exp_freq(INT8U freq)
{
	if(freq == 50)
	{
	seInfo.sensor_ev_idx = OV9750_25FPS_50HZ_INIT_EV_IDX;
	seInfo.ae_ev_step = 0;
	seInfo.daylight_ev_idx = OV9750_25FPS_50HZ_DAY_EV_IDX;
	seInfo.night_ev_idx = OV9750_25FPS_50HZ_NIGHT_EV_IDX;
	seInfo.max_ev_idx = OV9750_25FPS_50HZ_MAX_EV_IDX;
	seInfo.total_ev_idx = OV9750_25FPS_50HZ_EXP_TIME_TOTAL;

	p_expTime_table = (int *) ov9750_mipi_25fps_exp_time_gain_50Hz;
		DBG_PRINT(">>Set frequence 50Hz.\r\n");
	}
	else
	if(freq == 60)
	{
		seInfo.sensor_ev_idx = OV9750_25FPS_60HZ_INIT_EV_IDX;
		seInfo.ae_ev_step = 0;
		seInfo.daylight_ev_idx = OV9750_25FPS_60HZ_DAY_EV_IDX;
		seInfo.night_ev_idx =OV9750_25FPS_60HZ_NIGHT_EV_IDX;
		seInfo.max_ev_idx = OV9750_25FPS_60HZ_MAX_EV_IDX;
		seInfo.total_ev_idx = OV9750_25FPS_60HZ_EXP_TIME_TOTAL;

		p_expTime_table = (int *)ov9750_mipi_25fps_exp_time_gain_60Hz;

		DBG_PRINT(">>Set frequence 60Hz.\r\n");
	}
	sensor_set_max_lum(g_FavTable.max_lum);
}

void ov9750_mipi_seinfo_init(void)
{
	seInfo.sensor_ev_idx = OV9750_25FPS_50HZ_INIT_EV_IDX;
	seInfo.ae_ev_step = 0;
	seInfo.daylight_ev_idx = OV9750_25FPS_50HZ_DAY_EV_IDX;
	seInfo.night_ev_idx = OV9750_25FPS_50HZ_NIGHT_EV_IDX;
	seInfo.max_ev_idx = OV9750_25FPS_50HZ_MAX_EV_IDX;
	seInfo.total_ev_idx = OV9750_25FPS_50HZ_EXP_TIME_TOTAL;

	p_expTime_table = (int *) ov9750_mipi_25fps_exp_time_gain_50Hz;

	pre_sensor_time = 1;
	pre_sensor_a_gain = 0x00;

	seInfo.time = 1;
	seInfo.analog_gain = 0x00;
	seInfo.digital_gain = 0x20;
}




static void ov9750_mipi_sensor_init(void)
{
	INT8U val,val1;

	// Reset
#ifdef FRONT_SENSOR_RESET
	gpio_set_port_attribute(FRONT_SENSOR_RESET, ATTRIBUTE_HIGH);
	gpio_init_io(FRONT_SENSOR_RESET, GPIO_OUTPUT);
	gpio_write_io(FRONT_SENSOR_RESET, DATA_HIGH);
	OSTimeDly(5);	
	gpio_write_io(FRONT_SENSOR_RESET, DATA_LOW);
	OSTimeDly(5);
	gpio_write_io(FRONT_SENSOR_RESET, DATA_HIGH);
	OSTimeDly(5);
#endif

	// reguest sccb
	ov9750_sccb_open();


	ov9750_sccb_read(0x300b,&val);
	ov9750_sccb_read(0x300c,&val1);
	
	DBG_PRINT("OV9750_init,id:0x%02x%02x(need=0x9750)\n",val,val1);

	// init sensor
	ov9750_sccb_write_table((regval16_t *) ov9750_init_table);

	//DBG_PRINT("Sensor ov9750 mipi init completed\n");
}

/**
 * @brief   initialization function
 * @param   sensor format parameters
 * @return 	none
 */


void ov9750_cdsp_mipi_init(void)
{
	//DBG_PRINT("%s\n", __func__);

	//ae init
	ov9750_mipi_seinfo_init();
	//cdsp init
	drv_l2_cdsp_open();

	// mipi enable
#if (MIPI_DEV_NO == 1)
	drv_l1_mipi_init(MIPI_1, ENABLE);
#else
	drv_l1_mipi_init(MIPI_0, ENABLE);
#endif

}

/**
 * @brief   un-initialization function
 * @param   sensor format parameters
 * @return 	none
 */
static void ov9750_cdsp_mipi_uninit(void)
{
	//DBG_PRINT("%s\n", __func__);

	// disable mclk
	drv_l2_sensor_set_mclkout(MCLK_NONE, MASTER_CLOCK_MIPI_OFF, MASTER_CLOCK_CSI_OFF);

	// cdsp disable
	drv_l2_cdsp_close();
	// mipi disable
#if (MIPI_DEV_NO == 1)
	drv_l1_mipi_init(MIPI_1, DISABLE);
#else
	drv_l1_mipi_init(MIPI_0, DISABLE);
#endif

	// release sccb
	ov9750_sccb_close();
}

/**
 * @brief   stream start function
 * @param   info index
 *
 * @return 	none
 */
void ov9750_cdsp_mipi_stream_on(INT32U index, INT32U bufA, INT32U bufB)
{
	INT16U		target_w, target_h, sensor_w, sensor_h;
	gpCdspFmt_t format;

	// set sensor size
	//DBG_PRINT("%s = %d\n", __func__, index);

	/*Enable Mclk out*/
	drv_l1_csi_set_clock(ov9750_cdsp_mipi_ops.info[index].mclk, MASTER_CLOCK_MIPI_ON, MASTER_CLOCK_CSI_KEEP);

	//Enabel mipi clk, set mipi clk
	#if 0
	//drv_l2_mipi_ctrl_set_clk(ENABLE, 4);	//for GENERATE_PIXEL_CLK

	OSTimeDly(3);						//wait 3ms
	#endif

	/* cdsp clock */
#if MIPI_DEV_NO == 1
	drv_l1_CdspSetClockTree(ENABLE, MIPI1_INPUT, RAW);
#else
	drv_l1_CdspSetClockTree(ENABLE, MIPI_INPUT, RAW);
#endif

    // set cdsp format
#if MIPI_DEV_NO == 0
	format.image_source = C_CDSP_MIPI;
#else
	format.image_source = C_CDSP_MIPI1;
#endif
	format.input_format = ov9750_cdsp_mipi_ops.info[index].input_format;
	format.output_format = ov9750_cdsp_mipi_ops.info[index].output_format;
	target_w = ov9750_cdsp_mipi_ops.info[index].target_w;
	target_h = ov9750_cdsp_mipi_ops.info[index].target_h;
	format.hpixel = sensor_w = ov9750_cdsp_mipi_ops.info[index].sensor_w;
	format.vline = sensor_h = ov9750_cdsp_mipi_ops.info[index].sensor_h;
	format.hoffset =ov9750_cdsp_mipi_ops.info[index].hoffset;
	format.voffset = ov9750_cdsp_mipi_ops.info[index].voffset;
	format.sensor_timing_mode = ov9750_cdsp_mipi_ops.info[index].interface_mode;
	format.sensor_hsync_mode = ov9750_cdsp_mipi_ops.info[index].hsync_active;
	format.sensor_vsync_mode = ov9750_cdsp_mipi_ops.info[index].vsync_active;

	if(drv_l2_cdsp_set_fmt(&format) < 0)
	{
		DBG_PRINT("cdsp set fmt err!!!\n");
	}
	// set scale down
	/*
	if((format.hpixel > target_w) || (format.vline > target_h))
	{
		drv_l2_cdsp_set_yuv_scale(target_w, target_h);
	}
   */
	// cdsp start
	drv_l2_CdspTableRegister((gpCisCali_t *) &g_cali);
	drv_l2_cdsp_stream_on(ENABLE, bufA, bufB);		//Disable AE,AWB,SetSensor Task
	drv_l1_CdspSetDataSource(C_CDSP_SDRAM);
	drv_l2_cdsp_enable((gpUserFav_t*)&g_FavTable ,sensor_w, sensor_h, target_w, target_h);
	drv_l2_cdsp_sat_contrast_reinit_thr();


	//mipi start
#if (MIPI_DEV_NO == 1)
	if(drv_l1_mipi_set_parameter(MIPI_1, &ov9750_mipi_cfg) < 0)
	{
		DBG_PRINT("MIPI1 init fail !!!\n");
	}

	#if MIPI_ISR_TEST
		vic_irq_enable(VIC_MIPI_B);
		drv_l1_mipi_isr_register(mipi_ov9750_handle);
		drv_l1_mipi_set_irq_enable(MIPI_1, ENABLE, MIPI_INT_ALL);
	#endif
#else
	if(drv_l1_mipi_set_parameter(MIPI_0, &ov9750_mipi_cfg) < 0)
	{
		DBG_PRINT("MIPI0 init fail !!!\n");
	}
	else
	{
		//DBG_PRINT("MIPI0 init completed.\n");
		#if MIPI_ISR_TEST
			vic_irq_enable(VIC_MIPI);
			drv_l1_mipi_isr_register(mipi_ov9750_handle);
			drv_l1_mipi_set_irq_enable(MIPI_0, ENABLE, MIPI_INT_ALL);
	#endif
	}

#endif

	/*Init Sensor*/
	ov9750_mipi_sensor_init();

	// reset sensor ev idx
	seInfo.ae_ev_step = 0;
	ov9750_set_xfps_exposure_time(&seInfo);
	//DBG_PRINT("ov9750 mipi Exposure Time set!!\n");

	drv_l1_CdspSetDataSource(C_CDSP_MIPI);
	//DBG_PRINT("ov9750 cdsp mipi init done!!\n");

}

/**
 * @brief   stream stop function
 * @param   none
 * @return 	none
 */
static void ov9750_cdsp_mipi_stream_off(void)
{
	//drv_l2_cdsp_stream_off();
}

/**
 * @brief   get info function
 * @param   none
 * @return 	pointer to sensor information data
 */
static drv_l2_sensor_info_t *ov9750_cdsp_mipi_get_info(INT32U index)
{
	if(index > (MAX_INFO_NUM - 1))
	{
		return NULL;
	}
	else
	{
		return (drv_l2_sensor_info_t *) &ov9750_cdsp_mipi_ops.info[index];
	}
}

/*********************************************
*	sensor ops declaration
*********************************************/
const drv_l2_sensor_ops_t ov9750_cdsp_mipi_ops =
{
	SENSOR_SOURCE_MIPI,
	SENSOR_PATH_CDSP,
	ov9750_cdsp_mipi_init,
	ov9750_cdsp_mipi_uninit,
	ov9750_cdsp_mipi_stream_on,
	ov9750_cdsp_mipi_stream_off,
	NULL,
	NULL,
	ov9750_mipi_set_exp_freq,
	ov9750_cdsp_mipi_get_info,
	{
		/* 1st info */
		{
			MASTER_CLOCK_24MHZ,		/* MCLK */
			#if SENSOR_FLIP
			V4L2_PIX_FMT_SBGGR10,	/* input format */
			#else
			V4L2_PIX_FMT_SGBRG10,
			#endif
			V4L2_PIX_FMT_YUYV,		/* output format */
			25,						/* FPS in sensor */
			OV9750_OUT_WIDTH,		/* target width */
			OV9750_OUT_HEIGHT,		/* target height */
			OV9750_WIDTH,			/* sensor width */
			OV9750_HEIGHT,			/* sensor height */

			#if SENSOR_FLIP
			1,						/* sensor h offset */
			0,						/* sensor v offset */
			#else
			3,
			0,
			#endif

			MODE_CCIR_601,			/* input interface */
			MODE_NONE_INTERLACE,	/* interlace mode */
			MODE_ACTIVE_HIGH,		/* hsync pin active level */
			MODE_ACTIVE_HIGH,		/* vsync pin active level */
		}
	}
};
#endif //(defined _SENSOR_OV9750_CDSP_MIPI) && (_SENSOR_OV9750_CDSP_MIPI == 1)
